import { Request } from '../../../utils/request.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        //获取用户vip信息
        new Request('pay/Payorder/userInfoVip').request(
        () => {
            return {
                openid: app.globalData.openid, //用户的openid
                userid: app.globalData.userid, 
            }
        },
        (res) => {
         this.setData({
            userInfo: res.data.data
         });
        });
        //加载VIP列表
        new Request('pay/Payorder/vipList').request(
        () => {
            return {
                openid: app.globalData.openid, //用户的openid
            }
        },
        (res) => {
            this.lastArr = new Array();
            this.VIPLsit = new Array();
            res.data.data.map(function(e,n){
                
                if(n != 4){
                    this.VIPLsit.push(e);
                }else{
                    this.lastArr = e; 
                }
            }.bind(this));
            console.log(this.VIPLsit)
            this.setData({
                vipList: this.VIPLsit,
                VIPlast:this.lastArr,
            });
        });
    },
    //支付api
    onpay: function (param) {
        new Request('pay/Payorder/userPayOrder').request(
        () => {
            return {
                openid: app.globalData.openid, //用户的openid
                userid: app.globalData.userid,
                vipId: param.currentTarget.dataset.id,//查询购买的VIP等级
            }
        },
        (res) => {
            if(res.data.code == 0){
                new Request().showToast(res.data.msg);
                return;
            }

            new Request('pay/Pay/PayOrder').request(
                () => {
                    return {
                        order_id: res.data.data.order_id,
                        userid: app.globalData.userid,
                        price: res.data.data.price,
                        openid: getApp().globalData.openid,
                    };
                },
                (res) => {
                    wx.requestPayment({
                        timeStamp: res.data.timeStamp.toString(),
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: 'MD5',
                        paySign: res.data.sign,
                        success: function (res) {
                            new Request().showToast('支付成功','success');
                            app.globalData.userVip = param.currentTarget.dataset.id;
                            setTimeout(()=>{
                                wx.navigateBack({
                                    delta: 1
                                });
                            },1500)
                        },
                        fail: function (res) {
                            console.log(res);
                            wx.hideToast();
                            wx.showToast({
                                title: '支付已取消',
                                icon: 'none',
                                duration: 2000,
                            });
                        },
                    });
                }
            );
        });
    },
    /**
     * 跳转转角页面 详情页面
     */
    goto_vip_text:function(){
        app.globalData.click = true;
        wx.switchTab({
            url: '/pages/zhaon_zhuangjiao/zhaon_zhuangjiao',
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
});
